package parser.boc.bj08
import com.alipay.supergw.core.service.message.iso8583.ISOMessage
import org.apache.commons.lang.ArrayUtils

import com.alipay.supergw.common.util.lang.CoderUtil
import com.alipay.supergw.core.domain.shared.message.SupergwMessage
import com.alipay.supergw.core.service.message.iso8583.ISOMessage
import com.alipay.supergw.core.service.message.model.parser.SignatureInfo
import com.alipay.supergw.core.service.message.parser.ISO8583MessageParser
import com.alipay.supergw.core.service.message.util.POSUtil
import com.alipay.supergw.common.util.lang.StringUtil
import com.alipay.bcm.common.service.facade.model.enums.KeyTypeEnum

/**
 * 
 * 线下收单中行接入，签到回执
 * 
 * @author yanfen.liu
 * @version $Id:Bocbj_08_0801.java, v 0.1 2012-3-22 ????15:20:27 yanfen.liu Exp $
 */
public class Bocbj_08_0801 extends ISO8583MessageParser {

    private POSUtil      posUtil;

    private final String instId          = "BOC";

    private final String resCode_success = "00";

    /** 
     * @see com.alipay.supergw.core.service.message.parser.ISO8583MessageParser#getParseRules()
     */
    @Override
    public String getParseRules() {

      return """                 
        <message>
            <header length="7" />
            <field id="0" type="IFB_NUMERIC" length="4" pad="true" name="msgType" />
            <field id="1" type="IFB_BITMAP" length="8" />
            <field id="3" type="IFB_NUMERIC" length="6" name="processCode" />
            <field id="11" type="IFB_NUMERIC" length="6" name="outOrderNo" />
            <field id="12" type="IFB_NUMERIC" length="6" name="transactionTime" />
            <field id="13" type="IFB_NUMERIC" length="4" name="transactionDate" />
            <field id="24" type="IFB_NUMERIC" length="3" name="NII"  pad="true"/>
            <field id="37" type="IF_CHAR" length="12"  name="instReceiptNo"/>
            <field id="39" type="IF_CHAR" length="2" name="CHANNEL_RESPONSE_CODE" />
            <field id="41" type="IF_CHAR" length="8" name="termId" />
            <field id="42" type="IF_CHAR" length="15" name="merchantId" />
            <field id="48" type="IFB_LLLCHAR" length="999" name="workKey" />
            <field id="56" type="IFB_LLLCHAR" length="999" name="boc" />
            <field id="61" type="IFB_LLLCHAR" length="27" name="customDomain" />
        </message>
        """; 
     }

    /** 
     * @see com.alipay.supergw.core.service.message.parser.ISO8583MessageParser#parse(com.alipay.supergw.core.service.message.iso8583.ISOMessage)
     */
    @Override
    public SupergwMessage parse(ISOMessage message) {
       
        SupergwMessage gw = super.parse(message);  
        
        String customDomain = gw.g("customDomain");
        
        //处理批次号       
        if (customDomain.length() < 6) {
            throw new IllegalArgumentException("返回批次号长度有误");
        }        
        gw.addField("batchNo", StringUtil.substring(customDomain, 0, 6));            
        

       if(StringUtil.isNotBlank(gw.g("boc"))){
        gw.addField("boc", "");   //把它置空，否则解析报错
       }
        
        String responseCode = gw.g("CHANNEL_RESPONSE_CODE");
        if(StringUtil.equals("00", responseCode)){
            String workKey=gw.g("workKey");
            Map<String, String> keyMap = new HashMap<String, String>();
            keyMap.put(KeyTypeEnum.ZPK.getBizCode(), workKey.substring(5, 37));
            keyMap.put(KeyTypeEnum.ZAK.getBizCode(), workKey.substring(60, 92));
        
            Map<String, String> checkValueMap = new HashMap<String, String>();
           
            //数据库中要打开
           //posUtil.storeWorkKey(instId, gw.g("merchantId"), gw.g("termId"), keyMap, checkValueMap); 
        }
             
        return gw;
    }

    public void setPosUtil(POSUtil posUtil) {
        this.posUtil = posUtil;
    }
    
}